<?php
namespace Tools\Controller;
use Common\Controller\AdminbaseController;

class AdminAskController extends AdminbaseController{
	public function index(){
		if(!isset($_GET['p'])){
			$p = 1;
		}else{
			$p = $_GET['p'];
		}
		$count=M('tools_ask')->where("state != '1'")->count();
		$page = $this->page($count, 10);
		$ask = M('tools_ask')->where("state != '1'")->order("addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach($ask as $v){
			$user_id = $v['uid'];
			$username = M('users')->field("user_nicename")->where("id = '$user_id'")->find();
			$v['user'] = $username['user_nicename'];
			$a[] = $v;
		}
		$this->assign('p',$p);
		$this->assign("Page", $page->show('Admin'));
		$this->assign('ask',$a);
		$this->display();
	}

	//弹窗
	public function alert(){
		$id = $_POST['id'];
		$type = $_POST['type'];
		if($type == 'w'){
			$field = 'ask_content';
		}elseif($type == 'd'){
			$field = 'ask_d';
		}
		$data = M('tools_ask')->field($field)->where("id = '$id'")->find();
		echo $data[$field];die;
	}
	
	//回答
	public function answer(){
		$id = I('id');
		$data['ask_d'] = I('ask');
		$data['user_state'] = 1;
		$save = M('tools_ask')->where("id = '$id'")->save($data);
		if($save){
			echo 1;
		}else{
			echo 2;
		}
	}
	
	//封禁
	public function ban(){
		$id = I('id');
		$data['state'] = 2;
		$save = M('tools_ask')->where("id = '$id'")->save($data);
		if($save){
			$this->success('封禁成功！');
		}else{
			$this->error('封禁失败！');
		}
	}
	//解封
	public function jieban(){
		$id = I('id');
		$data['state'] = '0';
		$save = M('tools_ask')->where("id = '$id'")->save($data);
		if($save){
			$this->success('解封成功！');
		}else{
			$this->error('解封失败！');
		}
	}
	
	//删除
	public function delete(){
		$id = I('id');
		$data['state'] = '1';
		$save = M('tools_ask')->where("id = '$id'")->save($data);
		if($save){
			$this->success('删除成功！');
		}else{
			$this->error('删除失败！');
		}
	}
	
	public function hot(){
		$id = I('id');
		$data['hot'] = '1';
		$save = M('tools_ask')->where("id = '$id'")->save($data);
		if($save){
			$this->success('设置热门成功！');
		}else{
			$this->error('设置热门失败！');
		}
	}
	
	public function clearhot(){
		$id = I('id');
		$data['hot'] = '0';
		$save = M('tools_ask')->where("id = '$id'")->save($data);
		if($save){
			$this->success('取消热门成功！');
		}else{
			$this->error('取消热门失败！');
		}
	}
}